/*
 * Created by JFormDesigner on Thu Apr 14 16:16:32 CST 2022
 */

package com.ZengQi.event.view;

import com.ZengQi.event.controller.CompanyViewController;
import com.ZengQi.event.entity.Company;
import com.ZengQi.event.service.CompanyService;
import com.ZengQi.event.service.impl.CompanyServiceImpl;

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.Vector;
import javax.swing.*;
import javax.swing.GroupLayout;
import javax.swing.table.*;

/**
 * @author QI Zeng
 */
public class CompanyView extends JFrame {

    private String companyName;
    private String companyKey;
    private String companyDescription;

    private CompanyViewController companyViewController=new CompanyViewController();
    List<Company>list=null;

    /**
     * 查看企业信息的界面
     */
    public CompanyView() {

        initComponents();
        list=companyViewController.findCompanyList();
        if(list!=null){
            int i=0;
            DefaultTableModel dtm=(DefaultTableModel) table1.getModel();
            dtm.setRowCount(0);
            for(Company company:list){
                i=i+1;
                Vector<String> vector=new Vector<String>();
                vector.add(company.getCompanyName());
                vector.add(company.getCompanyKey());
                vector.add(company.getCompanyDescription());
                dtm.addRow(vector);
            }
        }
        setVisible(true);

        setLocationRelativeTo(null);

    }

    private void table1MousePressed(MouseEvent e) {
        tableMousePressed(e);
    }

    /**
     * 表格的鼠标事件监听
     * @param e
     */
    private void tableMousePressed(MouseEvent e) {
        int row=table1.getSelectedRow();
        Company company=list.get(row);
        companyNameTxt.setText(company.getCompanyName());
        companyKeyTxt.setText(company.getCompanyKey());
        companyDecriptionTxt.setText(company.getCompanyDescription());

        companyName=company.getCompanyName();
        companyKey=company.getCompanyKey();
        companyDescription=company.getCompanyDescription();
    }

    private void backBtn(ActionEvent e) {
        backBtnActionPerformed();
    }

    private void backBtnActionPerformed() {
        dispose();
    }

    /**
     * 组件布局及初始化
     */
    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        panel1 = new JPanel();
        companyNameLabel = new JLabel();
        label3 = new JLabel();
        companyNameTxt = new JTextField();
        companyKeyLabel = new JLabel();
        companyKeyTxt = new JTextField();
        companyDecriptionLabel = new JLabel();
        companyDecriptionTxt = new JTextField();
        button1 = new JButton();

        //======== this ========
        setResizable(false);
        setTitle("\u4f01\u4e1a\u4fe1\u606f");
        setIconImage(new ImageIcon(getClass().getResource("/bang.png")).getImage());
        Container contentPane = getContentPane();

        //======== scrollPane1 ========
        {

            //---- table1 ----
            table1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            table1.setModel(new DefaultTableModel(
                new Object[][] {
                    {null, null, null},
                    {null, null, null},
                    {null, null, null},
                    {null, null, null},
                },
                new String[] {
                    "\u4f01\u4e1a\u540d\u79f0", "\u4f01\u4e1a\u8003\u6838\u8981\u70b9", "\u4f01\u4e1a\u7b80\u4ecb"
                }
            ) {
                boolean[] columnEditable = new boolean[] {
                    false, false, false
                };
                @Override
                public boolean isCellEditable(int rowIndex, int columnIndex) {
                    return columnEditable[columnIndex];
                }
            });
            {
                TableColumnModel cm = table1.getColumnModel();
                cm.getColumn(0).setResizable(false);
                cm.getColumn(0).setPreferredWidth(250);
                cm.getColumn(1).setResizable(false);
                cm.getColumn(1).setPreferredWidth(185);
                cm.getColumn(2).setResizable(false);
                cm.getColumn(2).setMaxWidth(245);
                cm.getColumn(2).setPreferredWidth(245);
            }
            table1.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    table1MousePressed(e);
                }
            });
            scrollPane1.setViewportView(table1);
        }

        //======== panel1 ========
        {

            //---- companyNameLabel ----
            companyNameLabel.setText("\u4f01\u4e1a\u540d\u79f0");

            //---- companyNameTxt ----
            companyNameTxt.setEditable(false);

            //---- companyKeyLabel ----
            companyKeyLabel.setText("\u4f01\u4e1a\u8003\u6838\u8981\u70b9");

            //---- companyKeyTxt ----
            companyKeyTxt.setEditable(false);

            //---- companyDecriptionLabel ----
            companyDecriptionLabel.setText("\u4f01\u4e1a\u7b80\u4ecb");

            //---- companyDecriptionTxt ----
            companyDecriptionTxt.setEditable(false);

            //---- button1 ----
            button1.setText("\u8fd4\u56de");
            button1.addActionListener(e -> backBtn(e));

            GroupLayout panel1Layout = new GroupLayout(panel1);
            panel1.setLayout(panel1Layout);
            panel1Layout.setHorizontalGroup(
                panel1Layout.createParallelGroup()
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addGap(35, 35, 35)
                        .addGroup(panel1Layout.createParallelGroup()
                            .addGroup(panel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                .addComponent(companyNameLabel, GroupLayout.PREFERRED_SIZE, 106, GroupLayout.PREFERRED_SIZE)
                                .addComponent(companyKeyLabel, GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE))
                            .addGroup(panel1Layout.createSequentialGroup()
                                .addGap(53, 53, 53)
                                .addComponent(label3, GroupLayout.PREFERRED_SIZE, 76, GroupLayout.PREFERRED_SIZE))
                            .addGroup(panel1Layout.createSequentialGroup()
                                .addGap(6, 6, 6)
                                .addComponent(companyDecriptionLabel, GroupLayout.PREFERRED_SIZE, 102, GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(panel1Layout.createParallelGroup()
                            .addComponent(companyKeyTxt, GroupLayout.PREFERRED_SIZE, 274, GroupLayout.PREFERRED_SIZE)
                            .addComponent(companyNameTxt, GroupLayout.PREFERRED_SIZE, 162, GroupLayout.PREFERRED_SIZE)
                            .addGroup(panel1Layout.createSequentialGroup()
                                .addComponent(companyDecriptionTxt, GroupLayout.PREFERRED_SIZE, 348, GroupLayout.PREFERRED_SIZE)
                                .addGap(65, 65, 65)
                                .addComponent(button1)))
                        .addContainerGap(40, Short.MAX_VALUE))
            );
            panel1Layout.setVerticalGroup(
                panel1Layout.createParallelGroup()
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addGroup(panel1Layout.createParallelGroup()
                            .addComponent(companyNameTxt, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)
                            .addComponent(companyNameLabel, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(panel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(companyKeyTxt)
                            .addComponent(companyKeyLabel, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(panel1Layout.createParallelGroup()
                            .addGroup(panel1Layout.createSequentialGroup()
                                .addComponent(companyDecriptionLabel)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(label3))
                            .addGroup(panel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(companyDecriptionTxt, GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE)
                                .addComponent(button1)))
                        .addContainerGap())
            );
        }

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(79, 79, 79)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 715, GroupLayout.PREFERRED_SIZE)
                        .addComponent(panel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(79, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 196, GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addComponent(panel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 21, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JScrollPane scrollPane1;
    private JTable table1;
    private JPanel panel1;
    private JLabel companyNameLabel;
    private JLabel label3;
    private JTextField companyNameTxt;
    private JLabel companyKeyLabel;
    private JTextField companyKeyTxt;
    private JLabel companyDecriptionLabel;
    private JTextField companyDecriptionTxt;
    private JButton button1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
